package com.alon.spring01.proxy.cglib;

import com.alon.spring01.proxy.cglib.handler.MyInvocationHandler;
import com.alon.spring01.proxy.cglib.service.IHello;
import com.alon.spring01.proxy.cglib.service.impl.IHelloImpl;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.Proxy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @program: spring01
 * @description: 模拟客户端测试
 * @author: yulongguang
 * @create: 2019-08-19 11:54
 **/
public class MyProxyTest {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
            InstantiationException {
        // =========================第一种==========================
        // 1、生成$Proxy0的class文件
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        // 2、获取动态代理类
        Class proxyClazz = Proxy.getProxyClass(IHello.class.getClassLoader(), new Class[]{IHello.class});
        // 3、获得代理类的构造函数，并传入参数类型InvocationHandler.class
        Constructor constructor = proxyClazz.getConstructor(InvocationHandler.class);
        // 4、通过构造函数来创建动态代理对象，将自定义的InvocationHandler实例传入
        IHello iHello1 = (IHello) constructor.newInstance(new MyInvocationHandler(new IHelloImpl()));
        // 5、通过代理对象调用目标方法
        iHello1.sayHello();

// ==========================第二种=============================
        /**
         * Proxy类中还有个将2~4步骤封装好的简便方法来创建动态代理对象，
         *其方法签名为：newProxyInstance(ClassLoader loader,Class<?>[] instance, InvocationHandler h)
         */
        IHello iHello2 = (IHello) Proxy.newProxyInstance(IHello.class.getClassLoader(), // 加载接口的类加载器
                new Class[]{IHello.class}, // 一组接口
                new MyInvocationHandler(new IHelloImpl())); // 自定义的InvocationHandler
        iHello2.sayHello();



    }
}
